﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EndlessGalaxy
{
    public partial class DataView : Form
    {
        Planet here;
        int paddingx;
        int paddingy;
        public DataView(Planet here)
        {
            InitializeComponent();
            this.here = here;
            UpdateUI();
            paddingx = this.Width - tabControl1.Width;
            paddingy = this.Height - tabControl1.Height;
        }

        public void UpdateUI()
        {
            planetPage.Controls["label1"].Text = Globals.getData(here);
            while (tabControl1.TabCount > 1)
            {
                tabControl1.TabPages.RemoveAt(1);
            }
            while (planetPage.Controls["label1"].Height + planetPage.Controls["label1"].Top + 3 > planetPage.Height)
            {
                Height++;
                tabControl1.Height++;
                planetPage.Height++;
            }
            if (here.hasColony)
            {
                int n = 0;
                TabPage colonypage = new TabPage("Colony");
                colonypage.Size = planetPage.Size;
                Label colonyDetails = new Label();
                colonyDetails.Text = "Population: " + here.population;
                colonyDetails.Location = new Point(10, 10);
                colonypage.Controls.Add(colonyDetails);
                if (Math.Sqrt(4 * Math.PI * Math.Pow((here.Gravity() / ((4.0 / 3.0) * Math.PI)), (2.0 / 3.0))) >= (here.facilities.Count + 1) * 100)
                {
                    Button BuildFacility = new Button();
                    BuildFacility.Text = "Build\nFacility";
                    BuildFacility.Size = new System.Drawing.Size(75, 39);
                    BuildFacility.Location = new Point(10, 120);
                    BuildFacility.Tag = here;
                    BuildFacility.Click += BuildFacility_Click;
                    colonypage.Controls.Add(BuildFacility);
                }
                tabControl1.TabPages.Add(colonypage);
                foreach (Facility building in here.facilities)
                {
                    n++;
                    TabPage page = new TabPage("Facility " + n);
                    page.Size = planetPage.Size;
                    Label facilityDetails = new Label();
                    string deets = "Facility type: ";
                    if (building.miner)
                    {
                        bool notfirst = false;
                        deets += "Mining facility\nOutput: ";
                        if (building.output == null)
                        {
                            deets += "Surface Quarrying";
                        }
                        else
                        {
                            foreach (Cargo stuff in building.output)
                            {
                                if (notfirst)
                                {
                                    deets += " + ";
                                }
                                deets += stuff.name;
                            }
                        }
                    }
                    if (building.solar)
                    {
                        bool notfirst = false;
                        deets += "Solar collector\nOutput: ";
                        foreach (Cargo stuff in building.output)
                        {
                            if (notfirst)
                            {
                                deets += " + ";
                            }
                            notfirst = true;
                            deets += stuff.name;
                        }
                    }
                    if (building.shipbuilder)
                    {
                        deets += "Shipyard";
                    }
                    if ((!building.miner) && (!building.solar) && (!building.shipbuilder))
                    {
                        deets += "Factory/Foundry\nInput: ";
                        bool notfirst = false;
                        foreach (Cargo stuff in building.input)
                        {
                            if (notfirst)
                            {
                                deets += " + ";
                            }
                            notfirst = true;
                            deets += stuff.name;
                        }
                        deets += "\nOutput: ";
                        notfirst = false;
                        foreach (Cargo stuff in building.output)
                        {
                            if (notfirst)
                            {
                                deets += " + ";
                            }
                            notfirst = true;
                            deets += stuff.name;
                        }
                    }
                    facilityDetails.Text = deets;
                    facilityDetails.AutoSize = true;
                    facilityDetails.Location = new Point(10, 10);
                    page.Controls.Add(facilityDetails);
                    Button configbutton = new Button();
                    configbutton.Text = "Configure Facility";
                    configbutton.Size = new System.Drawing.Size(75, 39);
                    configbutton.Location = new Point(10, 120);
                    configbutton.Tag = building;
                    configbutton.Click += configbutton_Click;
                    page.Controls.Add(configbutton);
                    tabControl1.TabPages.Add(page);
                }
            }
            for (int i = 0; i < tabControl1.TabCount; i++)
            {
                tabControl1.TabPages[i].Refresh();
            }
        }
        public void LittleUpdate()
        {
            planetPage.Controls["label1"].Text = Globals.getData(here);
            planetPage.Refresh();
            tabControl1.Width = this.Width - paddingx;
            tabControl1.Height = this.Height - paddingy;
        }

        public void BuildFacility_Click(object sender, EventArgs e)
        {
            Globals.MainWindow.pauseTimer();
            FacilityBuilder config = new FacilityBuilder(here);
            config.ShowDialog();
            Globals.MainWindow.startTimer();
            UpdateUI();
        }

        public void configbutton_Click(object sender, EventArgs e)
        {
            Facility target = (sender as Button).Tag as Facility;
            Globals.MainWindow.pauseTimer();
            facilityConfig config = new facilityConfig(target);
            config.ShowDialog();
            Globals.MainWindow.startTimer();
            UpdateUI();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            LittleUpdate();
        }

    }
}
